
package com.netease.xone.dataMgr;

import com.netease.xone.R;

import android.content.Context;

public class CityManager {

    private String[] provinces;
    private Context mContext;
    private boolean mWithUnlimited;// 选项中是否包含无限

    public CityManager(Context context) {
        this(context, false);
    }

    public CityManager(Context context, boolean withUnlimited) {
        mContext = context;
        mWithUnlimited = withUnlimited;
        if (!withUnlimited) {
            provinces = context.getResources().getStringArray(R.array.china);
        } else {
            String[] tmpProvinces = context.getResources().getStringArray(R.array.china);
            provinces = new String[tmpProvinces.length + 1];
            provinces[0] = context.getString(R.string.filter_condition_unlimited);
            for (int i = 0; i < tmpProvinces.length; i++) {
                provinces[i + 1] = tmpProvinces[i];
            }
        }
    }

    public String[] getProvinces() {
        return provinces;
    }

    public String getProvinceString(int index) {
        return provinces[index];
    }

    public String getCityString(int provinceIndex, int cityIndex) {
        return getCity(provinces[provinceIndex]) != null ? getCity(provinces[provinceIndex])[cityIndex]
                : null;
    }

    public int getProvinceIndex(String targetProvince) {
        if (targetProvince == null || provinces == null)
            return -1;
        for (int i = 0; i < provinces.length; i++) {
            if (targetProvince.equals(provinces[i])) {
                return i;
            }
        }
        return 0;
    }

    public int getCityIndex(String targetProvice, String targetCity) {
        String[] cities = getCity(targetProvice);
        if (cities == null)
            return -1;
        for (int i = 0; i < cities.length; i++) {
            if (targetCity.equals(cities[i])) {
                return i;
            }
        }
        return 0;
    }

    public String[] getCity(String province) {
        if (province == null) {
            if (mWithUnlimited) {
                String[] tmpCity = new String[1];
                tmpCity[0] = mContext.getString(R.string.filter_condition_unlimited);
            } else {
                return null;
            }
        }
        String[] tmpCity = null;
        if (province.equals("北京")) {
            tmpCity = mContext.getResources().getStringArray(R.array.beijing);
        } else if (province.equals("上海")) {
            tmpCity = mContext.getResources().getStringArray(R.array.shanghai);
        } else if (province.equals("重庆")) {
            tmpCity = mContext.getResources().getStringArray(R.array.chongqing);
        } else if (province.equals("天津")) {
            tmpCity = mContext.getResources().getStringArray(R.array.tianjin);
        } else if (province.equals("安徽")) {
            tmpCity = mContext.getResources().getStringArray(R.array.anhui);
        } else if (province.equals("福建")) {
            tmpCity = mContext.getResources().getStringArray(R.array.fujian);
        } else if (province.equals("甘肃")) {
            tmpCity = mContext.getResources().getStringArray(R.array.gansu);
        } else if (province.equals("广东")) {
            tmpCity = mContext.getResources().getStringArray(R.array.guangdong);
        } else if (province.equals("广西")) {
            tmpCity = mContext.getResources().getStringArray(R.array.guangxi);
        } else if (province.equals("贵州")) {
            tmpCity = mContext.getResources().getStringArray(R.array.guizhou);
        } else if (province.equals("海南")) {
            tmpCity = mContext.getResources().getStringArray(R.array.hainan);
        } else if (province.equals("河北")) {
            tmpCity = mContext.getResources().getStringArray(R.array.hebei);
        } else if (province.equals("河南")) {
            tmpCity = mContext.getResources().getStringArray(R.array.henan);
        } else if (province.equals("黑龙江")) {
            tmpCity = mContext.getResources().getStringArray(R.array.heilongjiang);
        } else if (province.equals("湖北")) {
            tmpCity = mContext.getResources().getStringArray(R.array.hubei);
        } else if (province.equals("湖南")) {
            tmpCity = mContext.getResources().getStringArray(R.array.hunan);
        } else if (province.equals("吉林")) {
            tmpCity = mContext.getResources().getStringArray(R.array.jilin);
        } else if (province.equals("江苏")) {
            tmpCity = mContext.getResources().getStringArray(R.array.jiangsu);
        } else if (province.equals("江西")) {
            tmpCity = mContext.getResources().getStringArray(R.array.jiangxi);
        } else if (province.equals("辽宁")) {
            tmpCity = mContext.getResources().getStringArray(R.array.liaoning);
        } else if (province.equals("内蒙古")) {
            tmpCity = mContext.getResources().getStringArray(R.array.neimenggu);
        } else if (province.equals("宁夏")) {
            tmpCity = mContext.getResources().getStringArray(R.array.ningxia);
        } else if (province.equals("青海")) {
            tmpCity = mContext.getResources().getStringArray(R.array.qinghai);
        } else if (province.equals("山东")) {
            tmpCity = mContext.getResources().getStringArray(R.array.shandong);
        } else if (province.equals("山西")) {
            tmpCity = mContext.getResources().getStringArray(R.array.shanxi1);
        } else if (province.equals("陕西")) {
            tmpCity = mContext.getResources().getStringArray(R.array.shanxi3);
        } else if (province.equals("四川")) {
            tmpCity = mContext.getResources().getStringArray(R.array.sichuan);
        } else if (province.equals("西藏")) {
            tmpCity = mContext.getResources().getStringArray(R.array.xizang);
        } else if (province.equals("新疆")) {
            tmpCity = mContext.getResources().getStringArray(R.array.xinjiang);
        } else if (province.equals("云南")) {
            tmpCity = mContext.getResources().getStringArray(R.array.yunnan);
        } else if (province.equals("浙江")) {
            tmpCity = mContext.getResources().getStringArray(R.array.zhejiang);
        }

        if (!mWithUnlimited) {
            return tmpCity;
        } else {
            if (tmpCity == null) {
                String[] city = new String[1];
                city[0] = mContext.getString(R.string.filter_condition_unlimited);
                return city;
            } else {
                String[] city = new String[tmpCity.length + 1];
                city[0] = mContext.getString(R.string.filter_condition_unlimited);
                for (int i = 0; i < tmpCity.length; i++) {
                    city[i + 1] = tmpCity[i];
                }
                return city;
            }
        }

        // else {
        // if(mWithUnlimited){
        // String[] tmpCity = new String[1];
        // tmpCity[0] = mContext.getString(R.string.filter_condition_unlimited);
        // }else{
        // return null;
        // }
        // }
    }

}
